﻿using DataAccessLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BussinessLogicLayer
{
    public class NguoiDungBLL
    {
        static QuanLyGiaiDauDataContext QLGD = new QuanLyGiaiDauDataContext();
        public static List<NguoiDung> LayTatCa()
        {
            var nguoidung = from nd in QLGD.NguoiDungs
                           where nd.TrangThai != "Đã xóa"
                           select nd;
            return nguoidung.ToList();
        }
        public static List<NguoiDung> DangNhap(string TenDangNhap, string MatKhau)
        {
            var dangnhap = from nd in QLGD.NguoiDungs
                           where nd.TenDangNhap == TenDangNhap && nd.MatKhau == MatKhau
                           select nd;
            return dangnhap.ToList();
        }
        public static List<NguoiDung> LayMSNDTheoTenDN(string tenDN)
        {
            var LayMSND = from nd in QLGD.NguoiDungs
                          where nd.TenDangNhap == tenDN
                          select nd;
            return LayMSND.ToList();
        }
        public static bool ThemNguoiDung(NguoiDung entity)
        {
            NguoiDung ND = new NguoiDung();
            ND.HoTen = entity.HoTen;
            ND.GioiTinh = entity.GioiTinh;
            ND.NgaySinh = entity.NgaySinh;
            ND.TenDangNhap = entity.TenDangNhap;
            ND.MatKhau = entity.MatKhau;
            ND.CauHoiBiMat = entity.CauHoiBiMat;
            ND.CauTraLoi = entity.CauTraLoi;
            ND.TrangThai = entity.TrangThai;
            QLGD.NguoiDungs.InsertOnSubmit(ND);
            QLGD.SubmitChanges();
            int kq = 1;
            return kq > 0;
        }
        public static bool CapNhatNguoiDung(NguoiDung entity)
        {
            NguoiDung SuaND = (from nd in QLGD.NguoiDungs
                                      select nd).Single(ngdung => ngdung.MaNguoiDung == entity.MaNguoiDung);
            SuaND.HoTen = entity.HoTen;
            SuaND.GioiTinh = entity.GioiTinh;
            SuaND.NgaySinh = entity.NgaySinh;
            SuaND.TenDangNhap = entity.TenDangNhap;
            SuaND.MatKhau = entity.MatKhau;
            SuaND.CauHoiBiMat = entity.CauHoiBiMat;
            SuaND.CauTraLoi = entity.CauTraLoi;
            SuaND.TrangThai = entity.TrangThai;
            QLGD.SubmitChanges();
            int kq = 1;
            return kq > 0;
        }
        public static bool XoaNguoiDung(int MaND)
        {
            NguoiDung SuaND = (from nd in QLGD.NguoiDungs
                               select nd).Single(ngdung => ngdung.MaNguoiDung == MaND);
            SuaND.TrangThai = "Đã xóa";
            QLGD.SubmitChanges();
            int kq = 1;
            return kq > 0;
        }
    }
}
